package org.linlinjava.litemall.admin.web;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.LitemallCustomer;
import org.linlinjava.litemall.db.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@RequestMapping("/admin/customer")
@Validated
@Api(value = "客服", tags = "客服接口")
@RequiredArgsConstructor(onConstructor = @_(@Autowired))
public class AdminCustomerController {
    
    private final Log logger = LogFactory.getLog(AdminCustomerController.class);

    private final CustomerService customerService;

    @GetMapping("/list")
    @ApiOperation("客服列表查询")
    public Object list(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit) {
        List<LitemallCustomer> adList = customerService.querySelective(page, limit);
        return ResponseUtil.okList(adList);
    }

    @PostMapping("/create")
    @ApiOperation("新增banner")
    public Object create(@RequestBody LitemallCustomer customer) {
        customerService.add(customer);
        return ResponseUtil.ok(customer);
    }

    @GetMapping("/read")
    @ApiOperation("查询客服详情")
    public Object read(@NotNull Integer id) {
        LitemallCustomer customer = customerService.findById(id);
        return ResponseUtil.ok(customer);
    }

    @PostMapping("/update")
    @ApiOperation("更新banner")
    public Object update(@RequestBody LitemallCustomer customer) {
        if (null == customer.getId()) {
            return ResponseUtil.badArgument();
        }
        if (customerService.updateById(customer) == 0) {
            return ResponseUtil.updatedDataFailed();
        }

        return ResponseUtil.ok(customer);
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除客服信息")
    public Object delete(@PathVariable Integer id) {
        if (id == null) {
            return ResponseUtil.badArgument();
        }
        customerService.deleteById(id);
        return ResponseUtil.ok();
    }

}
